package com.hengxinguotong.zhihuichengjian.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.PersonnelRes;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.picturebrowser.view.TaskBigImgActivity;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.CircleImageView;
import com.lidroid.xutils.http.RequestParams;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/1.
 */

public class PersonnelActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.edit_tv)
    HXTextView editTv;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.avater_iv)
    CircleImageView avaterIv;
    @BindView(R.id.name_tv)
    HXTextView nameTv;
    @BindView(R.id.project_tv)
    HXTextView projectTv;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.profession_tv)
    HXTextView professionTv;
    @BindView(R.id.mobile_tv)
    HXTextView mobileTv;
    @BindView(R.id.emergency_contact_tv)
    HXTextView emergencyContactTv;
    @BindView(R.id.call_emergency_contact)
    HXTextView callEmergencyContact;
    @BindView(R.id.call_mobile)
    HXTextView callMobile;
    private String personId;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personnel);
        ButterKnife.bind(this);
        personId = getIntent().getStringExtra("personId");
        getPersonnelInfo();
    }

    @OnClick({R.id.iv_back, R.id.edit_tv, R.id.call_emergency_contact, R.id.call_mobile, R.id.avater_iv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.edit_tv:
                if (user != null) {
                    ActivityJumper.toAddPersonnelActivity(AddPersonnelActivity.MODTYPE, this, user);
                }
                break;
            case R.id.call_emergency_contact:
                if (Utils.isValidPhone(user.getIceContactMobile())) {
                    Utils.call(user.getIceContactMobile(), this);
                } else {
                    showToast("无效电话号码");
                }
                break;
            case R.id.call_mobile:
                if (Utils.isValidPhone(user.getMobile())) {
                    Utils.call(user.getMobile(), this);
                } else {
                    showToast("无效电话号码");
                }
                break;
            case R.id.avater_iv:
                if (user != null && user.getHeadPortrait() != null) {
                    ArrayList<String> paths = new ArrayList<String>();
                    paths.add(user.getHeadPortrait());
                    TaskBigImgActivity.startActivity(this, paths, 0);
                }
                break;
        }
    }


    private void getPersonnelInfo() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("personId", personId);
        Utils.requestData(getResources().getString(R.string.loading), this, "/person/detailInfo/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    PersonnelRes personnelRes = gson.fromJson(json, PersonnelRes.class);
                    user = personnelRes.getValue();
                    initView(user);
                }

            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    private void initView(User user) {
        if (!Utils.isEmpty(user.getHeadPortrait())) {
//            String headPortrait =Utils.SERVER_PIC + user.getHeadPortrait().substring(0, user.getHeadPortrait().indexOf(".")) + "_100x100.jpg";
            Glide.with(this).load(user.getHeadPortrait()).placeholder(R.mipmap.icon_avater_defalut).dontAnimate().error(R.mipmap.icon_avater_defalut).into(avaterIv);
        }
        nameTv.setText(user.getName());

        if (!Utils.isEmpty(user.getProjectCompanyName())) {
            projectTv.setText(user.getProjectCompanyName());
        }

        if (user.getTitle() > 0) {
            titleTv.setText(Constants.positionString[user.getTitle() - 1]);
        }
        if (user.getWorkType() > 0) {
            professionTv.setText(Constants.professionString[user.getWorkType() - 1]);
        }

        if (!Utils.isEmpty(user.getMobile())) {
            mobileTv.setText(user.getMobile());
        }

        if (!Utils.isEmpty(user.getIceContactMobile())) {
            emergencyContactTv.setText(user.getIceContactMobile() + "(紧急联系人)");
        }

    }
}
